/**
 * 
 */
package info.niwota.icoe;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

/**
 * @author qiangli
 * 
 */
public final class Preferences {

	public static final String PROFILE_NAME = "profile.name";
	public static final String PROFILE_NOTE = "profile.note";
	public static final String PROFILE_BLOODTYPE = "profile.bloodtype";

	public static final String CONTACT_NAME_ = "contact.name.";
	public static final String CONTACT_NOTE_ = "contact.note.";
	public static final String CONTACT_PHONE_ = "contact.phone.";
	public static final String CONTACT_RELATION_ = "contact.relation.";


	private Preferences() {
	}

	public static Profile getProfile(Context ctx) throws Exception {
		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
		Profile o = new Profile();
		o.name = p.getString(PROFILE_NAME, null);
		o.bloodType = p.getString(PROFILE_BLOODTYPE, null);
		o.note = p.getString(PROFILE_NOTE, null);
		return o;
	}
	
	
	public static void putProfile(Context ctx, Profile o) throws Exception {
		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);

		Editor e = p.edit();
		e.putString(PROFILE_NAME, o.name);
		e.putString(PROFILE_BLOODTYPE, o.bloodType);
		e.putString(PROFILE_NOTE, o.note);
		e.commit();
	}
	

	
	public static Contact getContact(Context ctx, String key) throws Exception {
		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);
		Contact o = new Contact();
		o.name = p.getString(CONTACT_NAME_+key, null);
		o.note = p.getString(CONTACT_NOTE_+key, null);
		o.phone = p.getString(CONTACT_PHONE_+key, null);
		o.relation = p.getString(CONTACT_RELATION_+key, null);
		return o;
	}
	
	public static void putContact(Context ctx, String key, Contact o) throws Exception {
		final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(ctx);

		Editor e = p.edit();
		e.putString(CONTACT_NAME_+key, o.name);
		e.putString(CONTACT_NOTE_+key, o.note);
		e.putString(CONTACT_PHONE_+key, o.phone);
		e.putString(CONTACT_RELATION_+key, o.relation);
		e.commit();
	}
}
